function resizeKanban(){
	var bodyHeight = $(window).height();

	if(bodyHeight < 500){
		$("#box_content").height(385);
	}else{
		var topHeight = 126;
		$("#box_content").height(bodyHeight - topHeight);
	}
}

$(window).resize(function() {
	resizeKanban();
});

$(document).ready(function(){
	loadTasksStates();
	resizeKanban();

	$("#newTaskState").click(function(){
		newTaskState();
	})
});

function showModal(){
	resizeKanban();
}

function closeModal(){
	resizeKanban();
}


/******************************************************************************************************************
Estados das Tarefas Functions
******************************************************************************************************************/
function loadTasksStates(){
	$.ajax({
		url: SITE+"app/Views/V_Task.php?f=getTasksStates",
		async: false,
		success: function(data){
			var obj = JSON.decode(data);
			if(obj[0]){
				$(".statusTasks").html(obj[1]);

				$("#sortable").sortable({
					cancel: ".disabled",
					update: function(event, ui) {
						var ordersStates = "";
						$("#sortable li").each(function(){
							if(ordersStates != "")
								ordersStates += ",";
						
							ordersStates += $(this).attr("id").replace("taskState_","");
						})

						$.ajax({
							url: SITE+"app/Views/V_Task.php?f=changeOrderTaskState&order="+ordersStates,
							success: function(data){
							}
						}); 
					}
				});
				$("#sortable").disableSelection();
			}else{
				resultMensage(obj, true);
			}			
		}
	});

	return false;
}

function newTaskState(){
	$.ajax({
		url: SITE+"app/Views/V_Task.php?f=newTaskState",
		success: function(data){
			var obj = JSON.decode(data);
			loadHTMLTaskState(obj);
		}
	});
}


function editTaskState(idTaskState){
	if(idTaskState > 0){
		$.ajax({
			url: SITE+"app/Views/V_Task.php?f=editTaskState&idTaskState="+idTaskState,
			success: function(data){
				var obj = JSON.decode(data);
				loadHTMLTaskState(obj);
			}
		});
	}
}

function deleteTaskState(idTaskState){
	openBox("modalDelete");

	$("#modalDelete #confirm").click(function(){
		$.ajax({
			url: SITE+"app/Views/V_Task.php?f=deleteTaskState&idTaskState="+idTaskState,
			success: function(data){
				var obj = JSON.decode(data);
				if(obj[0]){
					$("#taskState_"+idTaskState).remove();					
				}
				resultMensage(obj, true);
			}
		});
	});
}


function loadHTMLTaskState(obj){
	if(obj[0]){
		openBox("overlay", obj[1]);

		$("#formTaskState").validate({
			submitHandler: function(form) {
				var query = $(form).serialize();
				
				$.ajax({
					url: SITE+"app/Views/V_Task.php?f=saveTaskState&"+query,
					async: false,
					success: function(data){
						var obj = JSON.decode(data);
						resultMensage(obj, true);
						loadTasksStates();
					}
				});			
			}
		});
	}else{
		resultMensage(obj, true);
	}
}